package fw.qaz.asmx.reader.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.TextView;

import java.util.ArrayList;

import fw.qaz.asmx.reader.R;

public class Dialogs
{
    Context context;

    public Dialogs(Context context)
    {
        this.context = context;
    }


    //选取颜色的dialog
    AlertDialog dialog;

    //保存颜色的key--------现有的是bgColor或者fontColor
    public AlertDialog SelectColorAlertDialog(final String keyname)
    {
        //读写设置时需要
        SharedPreferences sp = context.getSharedPreferences("preferences", 0);
        ;//使用的SharedPreferences（用默认的，在 /data/data/包名/shared_prefs/preferences.xml）
        final SharedPreferences.Editor edit = sp.edit();


        //动态加载布局生成View对象
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        View SelectColorlayout = layoutInflater.inflate(R.layout.select_color_dialog, null);
        final SeekBar seekbarR = (SeekBar) SelectColorlayout.findViewById(R.id.seekBarColorR);
        final SeekBar seekbarB = (SeekBar) SelectColorlayout.findViewById(R.id.seekBarColorB);
        final SeekBar seekbarG = (SeekBar) SelectColorlayout.findViewById(R.id.seekBarColorG);

        final TextView textR = (TextView) SelectColorlayout.findViewById(R.id.textViewColorR);
        final TextView textB = (TextView) SelectColorlayout.findViewById(R.id.textViewColorB);
        final TextView textG = (TextView) SelectColorlayout.findViewById(R.id.textViewColorG);

        final ImageView imageView =
                (ImageView) SelectColorlayout.findViewById(R.id.imageViewColorPreview);


        //滑动时改变文本框的值


        seekbarR.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
        {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b)
            {
                textR.setText(String.valueOf(i));
                imageView.setBackgroundColor(
                        Color.argb(255, seekbarR.getProgress(), seekbarG.getProgress(),
                                   seekbarB.getProgress()));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar)
            {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar)
            {

            }
        });
        seekbarB.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
        {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b)
            {
                textB.setText(String.valueOf(i));
                imageView.setBackgroundColor(
                        Color.argb(255, seekbarR.getProgress(), seekbarG.getProgress(),
                                   seekbarB.getProgress()));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar)
            {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar)
            {

            }
        });
        seekbarG.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
        {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b)
            {
                textG.setText(String.valueOf(i));
                imageView.setBackgroundColor(
                        Color.argb(255, seekbarR.getProgress(), seekbarG.getProgress(),
                                   seekbarB.getProgress()));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar)
            {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar)
            {
            }
        });

        //设置文件中获取设置
        seekbarR.setProgress(sp.getInt(keyname + "Red", 0));
        seekbarB.setProgress(sp.getInt(keyname + "Blue", 0));
        seekbarG.setProgress(sp.getInt(keyname + "Green", 0));

        AlertDialog dialog = new AlertDialog.Builder(context)
                .setTitle(R.string.choose_color_rbg)
                .setView(SelectColorlayout)                //加载自定义的对话框式样
                .setPositiveButton(android.R.string.yes, null)
                .setNeutralButton(android.R.string.cancel, null)
                .create();

        //消失的时候写入设置
        dialog.setOnDismissListener(new DialogInterface.OnDismissListener()
        {
            @Override
            public void onDismiss(DialogInterface dialogInterface)
            {
                edit.putInt(keyname + "Red", seekbarR.getProgress())
                    .putInt(keyname + "Blue", seekbarB.getProgress())
                    .putInt(keyname + "Green", seekbarG.getProgress())
                    .apply();
            }
        });

        return dialog;
    }


    public AlertDialog Menu2(){
        //阅读界面里的菜单
        LinearLayout linearLayoutMain = new LinearLayout(context);//自定义一个布局文件
        linearLayoutMain.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        ListView listView = new ListView(context);//this为获取当前的上下文
        listView.setFadingEdgeLength(0);
        listView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        ArrayList<String> menuString = new ArrayList<String>();
        menuString.add("添加书签");
        menuString.add("书签列表");

        listView.setAdapter(new ArrayAdapter<String>(context, android.R.layout.simple_expandable_list_item_1, menuString));
        //listView.setAdapter(adapter);

        linearLayoutMain.addView(listView);//往这个布局中加入listview

        final AlertDialog md = new AlertDialog.Builder(context)
                //.setTitle("你好")
                .setView(linearLayoutMain)//不要标题
                .create();

        md.setCanceledOnTouchOutside(true);//使除了dialog以外的地方不能被点击
        md.show();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            //响应listview中的item的点击事件
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
                Log.i("mao","aaa");

                switch (arg2) {
                    case 0: //保存书签
                        //SettingCenter.saveBookmark();
                        break;

                    case 1: //打开书签列表
                        md.dismiss();
                        bookMarksDialog();
                        break;

                }
            }
        });
        return md;
    }


    public AlertDialog bookMarksDialog(){

        //阅读界面里的菜单
        LinearLayout linearLayoutMain = new LinearLayout(context);//自定义一个布局文件
        linearLayoutMain.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        ListView listView = new ListView(context);//this为获取当前的上下文
        listView.setFadingEdgeLength(0);
        listView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        ArrayList<String> menuString = new ArrayList<String>();
        menuString.add("书签1");
        menuString.add("书签2");
        //这里添加书签


        listView.setAdapter(new ArrayAdapter<String>(context, android.R.layout.simple_expandable_list_item_1, menuString));
        //listView.setAdapter(adapter);

        linearLayoutMain.addView(listView);//往这个布局中加入listview

        final AlertDialog md = new AlertDialog.Builder(context)
                .setView(linearLayoutMain)
                .create();

        md.show();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            //响应listview中的item的点击事件
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {

            }
        });

        return md;

    }




}
